﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WcfTwitterChatService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class TwitterChatService : ITwitterChatService
    {
        private int lastMessageID;

        public IEnumerable<MessageDTO> GetNewMessages(string channel)
        {
            TwitterChatEntities context = new TwitterChatEntities();
            var messages = (from m in context.Messages
                            join c in context.Channels on m.ChannelID equals c.ID
                            where c.Name == channel && m.ID > lastMessageID
                            select m).ToList();
            lastMessageID = messages[messages.Count - 1].ID;
            IEnumerable<MessageDTO> messagesDTOs =
                messages.Select(m => MessageDTO.CreateFromMessage(m));
            return messagesDTOs;
        }


        public IEnumerable<MessageDTO> GetAllMessagesInChannel(string channel)
        {
            TwitterChatEntities context = new TwitterChatEntities();
            var messages = (from m in context.Messages
                            join c in context.Channels on m.ChannelID equals c.ID
                            where c.Name == channel
                            orderby m.ID descending
                            select m).Take(15).ToList();
            lastMessageID = messages[0].ID;
            var mmessages = messages.OrderBy(m => m.ID);
            IEnumerable<MessageDTO> messagesDTOs =
                mmessages.Select(m => MessageDTO.CreateFromMessage(m));
            return messagesDTOs;
        }

        public void PostMessage(Msg m)
        {
            TwitterChatEntities context = new TwitterChatEntities();
            Message msg = new Message();
            msg.Text = m.Text;
            int channelID = (from c in context.Channels
                             where c.Name == m.Channel
                             select c.ID).SingleOrDefault();
            msg.ChannelID = channelID;
            msg.PostingDateTime = m.PostingDate;
            context.Messages.AddObject(msg);
            context.SaveChanges();
        }

        public void CreateNewChannel(string name)
        {
            TwitterChatEntities context = new TwitterChatEntities();
            var channel = (from c in context.Channels
                           where c.Name == name
                           select c);
            bool channelExist = channel.Any();
            if (channelExist)
            {
                int channelID = (from c in context.Channels
                                 where c.Name == name
                                 select c.ID).SingleOrDefault();
                List<Message> msgsToDelete = context.Messages.Where(m => m.ChannelID == channelID).ToList();
                foreach (var msg in msgsToDelete)
                {
                    context.DeleteObject(msg);
                }
            }
            else
            {
                Channel channelToAdd = new Channel();
                channelToAdd.Name = name;
                context.Channels.AddObject(channelToAdd);
            }
            context.SaveChanges();
        }

        public IEnumerable<ChannelDTO> GetAllChannels()
        {
            TwitterChatEntities context = new TwitterChatEntities();
            var channels = context.Channels.OrderBy(c => c.Name).ToList();
            IEnumerable<ChannelDTO> channelsDTOs =
                channels.Select(c => ChannelDTO.CreateFromChannel(c));
            return channelsDTOs;
        }
    }
}
